package flc.ast.activity;

import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.io.File;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityPreviewBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class PreviewActivity extends BaseAc<ActivityPreviewBinding> {

    public static boolean isVideo = false;
    public static Bitmap resultImg;//拍照结果
    public static String resultPath;//视频结果

    @Override
    protected int onCreate() {
        return R.layout.activity_preview;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPreviewBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.ivVideoPlay.setOnClickListener(this);
        mDataBinding.ivSave.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.ivVideoPlay) {
            if (mDataBinding.videoView.isPlaying()) {
                mDataBinding.ivVideoPlay.setImageResource(R.drawable.abof);
                mDataBinding.videoView.pause();
            } else {
                mDataBinding.ivVideoPlay.setImageResource(R.drawable.azant);
                mDataBinding.videoView.start();
            }
        } else if (id == R.id.ivSave) {
            savePreview();
        }
    }

    @Override
    protected void initData() {
        if (isVideo) {
            mDataBinding.ivPreviewImg.setVisibility(View.GONE);
            mDataBinding.rlVideo.setVisibility(View.VISIBLE);
            setPlayer();
        } else {
            mDataBinding.ivPreviewImg.setVisibility(View.VISIBLE);
            mDataBinding.rlVideo.setVisibility(View.GONE);
            mDataBinding.ivPreviewImg.setImageBitmap(resultImg);
        }
    }

    //保存预览
    private void savePreview() {
        showDialog(getString(R.string.save_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        if (isVideo) {
                            //保存到相册
                            FileP2pUtil.copyPrivateVideoToPublic(mContext, resultPath);
                            String filePath = FileUtil.generateFilePath(Content.myRecord, "." + FileUtils.getFileExtension(resultPath));
                            FileUtils.copy(resultPath, filePath);
                        } else {
                            //保存到相册
                            File file = ImageUtils.save2Album(resultImg, Bitmap.CompressFormat.PNG);
                            String filePath = FileUtil.generateFilePath(Content.myRecord, "." + FileUtils.getFileExtension(file.getPath()));
                            FileUtils.copy(file.getPath(), filePath);
                        }
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

    //设置播放器
    private void setPlayer() {
        //设置播放器
        mDataBinding.videoView.setVideoPath(resultPath);
        mDataBinding.videoView.seekTo(1);
        mDataBinding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {  // 播放完成的监听
                mDataBinding.ivVideoPlay.setImageResource(R.drawable.abof);
                mp.seekTo(1);
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (isVideo) {
            mDataBinding.videoView.pause();
            mDataBinding.ivVideoPlay.setImageResource(R.drawable.abof);
        }
    }
}